package com.pureut.equipment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.equipment.domain.EquipmentKnowledge;
import com.pureut.equipment.domain.dto.EquipmentKnowledgeDto;
import com.pureut.equipment.domain.vo.EquipmentKnowledgeVo;
import com.pureut.equipment.mapper.EquipmentKnowledgeMapper;
import com.pureut.equipment.service.IEquipmentKnowledgeService;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.core.utils.DateUtils;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.system.api.RemoteCodeService;
import com.sale.system.api.domain.SysDictData;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Date 23-04-23
 * @Author hl
 * 设备知识库实现层
 **/
@Service
public class EquipmentKnowledgeServiceImpl extends ServiceImpl<EquipmentKnowledgeMapper, EquipmentKnowledge> implements IEquipmentKnowledgeService {

    @Resource
    private EquipmentKnowledgeMapper equipmentKnowledgeMapper;

    @DubboReference
    RemoteCodeService remoteCodeService;

    /**
     * 根据设备知识库列表
     *
     * @param equipmentKnowledgeVo 接受类
     * @return 设备知识库列表
     */
    @Override
    public TableDataInfo<EquipmentKnowledgeDto> selectKnowledgeList(EquipmentKnowledgeVo equipmentKnowledgeVo, PageQuery pageQuery) {
        Page<EquipmentKnowledgeDto> equipmentKnowledgeDtoPage = equipmentKnowledgeMapper.selectKnowledgeList(equipmentKnowledgeVo, pageQuery.build());

        //获取设备类型字典
        List<SysDictData> deviceTypeArray = DictUtils.getDictCache("device_type");
        Map<String, String> deviceTypeMap = deviceTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //获取故障类别字典
        List<SysDictData> failureArray = DictUtils.getDictCache("equipment_failure_type");
        Map<String, String> failureMap = failureArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<EquipmentKnowledgeDto> records = equipmentKnowledgeDtoPage.getRecords();
        for (EquipmentKnowledgeDto entity : records) {
            //设备类型
            entity.setDeviceTypeDict(deviceTypeMap.get(entity.getDeviceType()));
            //故障类型
            entity.setKnowledgeFailureTypeDict(failureMap.get(entity.getKnowledgeFailureType()));
        }

        return TableDataInfo.build(equipmentKnowledgeDtoPage);
    }

    /**
     * 新增设备知识库
     *
     * @param equipmentKnowledge
     * @return 设备知识库新增
     */
    @Override
    public boolean insertEquipmentKnowledge(EquipmentKnowledge equipmentKnowledge) throws Exception {
        equipmentKnowledge.setCreateTime(DateUtils.getTime());
        equipmentKnowledge.setCreateBy(LoginHelper.getUsername());
        equipmentKnowledge.setDeptId(String.valueOf(LoginHelper.getDeptId()));
        String authorityCoding = remoteCodeService.authorityCoding("device:repair:knowledge:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        equipmentKnowledge.setKnowledgeCode(authorityCoding);
        return save(equipmentKnowledge);
    }

    /**
     * 修改设备知识库
     *
     * @param equipmentKnowledge
     * @return 设备知识库修改
     */
    @Override
    public boolean updateEquipmentKnowledge(EquipmentKnowledge equipmentKnowledge) {
        return updateById(equipmentKnowledge);
    }

    /**
     * 查询单个设备知识库
     *
     * @param knowledgeId id
     * @return 查询单个
     */
    @Override
    public EquipmentKnowledgeDto selectKnowledgeById(long knowledgeId) {
        //获取设备类型字典
        List<SysDictData> deviceTypeArray = DictUtils.getDictCache("device_type");
        Map<String, String> deviceTypeMap = deviceTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        EquipmentKnowledgeVo vo = new EquipmentKnowledgeVo();
        vo.setKnowledgeId(knowledgeId);
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(1000);
        pageQuery.setPageSize(9999);
        EquipmentKnowledgeDto equipmentKnowledgeDto = equipmentKnowledgeMapper.selectKnowledgeList(vo, pageQuery.build()).getRecords().get(0);
        equipmentKnowledgeDto.setDeviceTypeDict(deviceTypeMap.get(equipmentKnowledgeDto.getDeviceType()));
        return equipmentKnowledgeDto;
    }

    /**
     * 删除单个设备知识库
     *
     * @param knowledgeId id
     * @return 删除单个
     */
    @Override
    public boolean deleteKnowledgeById(long knowledgeId) {
        return removeById(knowledgeId);
    }

    /**
     * map列表
     *
     * @return
     */
    @Override
    public List<Map<String, String>> selectKnowledgeById1() {
        List<Map<String, String>> list = equipmentKnowledgeMapper.selectKnowledgeById1();
        return list;
    }


    /**
     * 首字母大写
     */
    public static String upCaseFirstChar(String str) {
        char[] chars = str.toCharArray();
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            chars[0] = (char) (chars[0] - 32);
        }
        return new String(chars);
    }

}
